package com.xb.loan.usercenter.user.task;

import com.xb.loan.usercenter.service.user.IWinstantSessionService;
import com.xb.loan.usercenter.service.user.bean.WinstantLoginInfoVO;
import com.xb.loan.usercenter.service.user.bean.WinstantOnlineUserVO;
import com.xb.loan.usercenter.service.user.constant.UserConst;
import com.xb.loan.util.cache.redis.collection.RedisList;
import com.xb.loan.util.cache.redis.collection.Rlist;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import com.xb.loan.util.log.LogFormatUtil;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import org.slf4j.Logger;

/**
 *
 * 项目名称 : service-usercenter-biz
 * 类  描  述 : 用户在线统计任务
 */
public class StatOnlineUserTask extends AbstractCronTask {

    private Logger logger = DefaultLoggerFacotry.getLogger(StatOnlineUserTask.class);

    private IWinstantSessionService winstantSessionService;
    
    private static Rlist<String> onlineUserList = new RedisList<String>("OnlineUser", String.class, true);

    public StatOnlineUserTask() {
        super();
    }

    public StatOnlineUserTask(String cron) {
        //设置任务名称
        this.setJobName("StatOnlineUserTask");
        //设置执行时间策略
        this.setCron_expression(cron);
    }

    @Override
    public void run() {

        winstantSessionService = getSpringBean("winstantSessionService");

        logger.info(LogFormatUtil.getFormatMsg(new String[]{"module", "message"},
				new String[]{"StatOnlineUserTask", "start ..."}));

        try {
        	
            int size = onlineUserList.size();
            int realCount = 0;
            int noRealCount = 0;
            WinstantOnlineUserVO onlineUserVO = new WinstantOnlineUserVO();
            
            for(int i=size;i>0;i--){                
                
            	String sessionId = onlineUserList.get(i-1);
            	WinstantLoginInfoVO loginInfoVO = winstantSessionService.queryUserInfoCacheOnly(sessionId);
            	
    			if(loginInfoVO != null){
            		long lastAccessTime = loginInfoVO.getTokenEffective().getTime();
					//long expiredTime = loginInfoVO.getExpiredTime();
					long currentTime = System.currentTimeMillis();

					if(lastAccessTime < currentTime){
						
						//改为同步更新
						winstantSessionService.clearUserInfoCache(sessionId);
						onlineUserList.remove(sessionId);
					}else{
						
						if(loginInfoVO.getRealFlag() == UserConst.UserRealFlag.NOREAL.getCode()){
							
							noRealCount = noRealCount + 1;
						}else{
							
							realCount = realCount + 1;
						}
					}
            	}else{
        			
            		//正常退出或者强制退出
            		onlineUserList.remove(sessionId);
        		}
            }    
            onlineUserVO.setNoRealCount(noRealCount);
            onlineUserVO.setRealCount(realCount);
            winstantSessionService.updateOnlineUserVO(onlineUserVO);
        } catch (Exception e) {

        	logger.error(LogFormatUtil.getFormatMsg(new String[]{"module", "message"},
					new String[]{"StatOnlineUserTask", "run have an exception ..."}),e);
        }
        logger.info(LogFormatUtil.getFormatMsg(new String[]{"module", "message"},
				new String[]{"StatOnlineUserTask", "end ..."}));
    }
    
}
